package jass.iss.edu.Screen;

import jass.iss.edu.Properties.*;

import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
 * Author 		: Lee Zhong De Rollei
 * Student ID	: A0079953L
 */
public class GridControl extends JPanel{

	/*Local attributes=================================================================================================*/
	//required as it references serializable objects for binary saving
	private static final long serialVersionUID = 1L;
	//Grid variables
	private List<JobAdvertisementDTO> objList;
	private String[] columnNames;
	private String[][] dataValues;
	
	// UI variables
	private	JTable		grid;
	private	JScrollPane scrollPane;
	


	/*Constructors=======================================================================================================*/
	public GridControl()
	{ 
		JobAdvertisementDAO j = null;
		try {
			j = new JobAdvertisementDAO();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setSize(500,500);		
		this.objList =  j.getAll();

		initializeGrid();		
	}

	
	public JTable getGrid() {
		return grid;
	}

	/*Methods to initialize GridControl =================================================================================*/	
	private void initializeGrid(){	
		//Create all required columns

		
	   //declare local variables***********************************************************
	   String recFoundMsg;	   
	   
	   //define column used to bind JTable based on gridColumn collection******************
	   columnNames = new String[8];	
	   columnNames[0]="S/N";
	   columnNames[1]="Ref No";
	   columnNames[2]="Description";
	   columnNames[3]="Requirements";
	   columnNames[4]="Status";
	   columnNames[5]="";
	   columnNames[6]="";
	   columnNames[7]="";
	   		
	   //define data value used to bind JTable based on object collection******************
		dataValues = new String[objList.size()][8];
		for(int i=0;i<objList.size();i++){
			JobAdvertisementDTO advert = objList.get(i);		
			dataValues[i] = GetRowData(advert);
		}

		//initialize JTable*****************************************************************
		initializeJTable();
		
		//define records found message******************************************************
		recFoundMsg = " Displaying "+String.valueOf(objList.size())+" out of "+String.valueOf(objList.size())+" record(s) Found !";
		
		//Create GridControl Panel**********************************************************
		setLayout(new BorderLayout());
		this.removeAll();

		this.add(scrollPane,BorderLayout.CENTER);	
		this.add(new JLabel(recFoundMsg),BorderLayout.SOUTH);
		this.repaint();
		this.validate();
	}

	private void initializeJTable(){
		//Create Model with uneditable cell as the table is only for viewing
		TableModel model = new DefaultTableModel(dataValues, columnNames){
			private static final long serialVersionUID = 1281697969128810817L;

		public boolean isCellEditable(int rowIndex, int colIndex) {
	          return false;
	        }
	      };
		

	    //RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
		grid = new JTable(model);

		//Set column width Width		
		TableColumn column = grid.getColumnModel().getColumn(0);
		column.setPreferredWidth(20);			
		
		//add JTable to scrolling panel
		scrollPane = new JScrollPane(grid);
	}
	
	//Based on the columns and object collection. Reflect the object to get the object value
	private String[] GetRowData(JobAdvertisementDTO pAdvert) {
		String[] row = new String[8];
		row[0] = pAdvert.getId();
		row[1] = pAdvert.getReferenceID();
		row[2] = pAdvert.getJobDescription();
		row[3] = pAdvert.getRequirements();
		row[4] = pAdvert.getJobAdvertStatus();
		row[5] = "Modify";
		row[6] = "Withdraw";
		row[7] = "Delete";

		
		return row;
	}

	
	public void refreshList(){
	}

	

}
